package ar.com.ford.it.fullEconomics.persistence.dao.query;

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import org.apache.commons.lang.StringUtils;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese05Paypolvers;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class PaymentPolicieVersFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teese05Paypolvers payPolVers = (Teese05Paypolvers) parameters[0];
        String mode = (String) parameters[1];

        String supplierClass;
        String supplierCode;
        Integer itemCat;
        Integer item;
        if (payPolVers.getTeese04Paypolicy().getTeess01Supplier() != null) {
            supplierClass = payPolVers.getTeese04Paypolicy()
                    .getTeess01Supplier().getEess01SupplierClassCodeC();
            supplierCode = payPolVers.getTeese04Paypolicy()
                    .getTeess01Supplier().getEess01SupplierCodeC();
        } else {
            supplierClass = "-1";
            supplierCode = null;
        }
        if (payPolVers.getTeese04Paypolicy().getTeesc05Itemcatgry() != null) {
            itemCat = payPolVers.getTeese04Paypolicy().getTeesc05Itemcatgry()
                    .getEesc05ItemcatgryNumberK();
        } else {
            itemCat = -1;
        }
        if (payPolVers.getTeese04Paypolicy().getTeesc06Item() != null) {
            item = payPolVers.getTeese04Paypolicy().getTeesc06Item()
                    .getEesc06ItemNumberK();
        } else {
            item = -1;
        }

        Teesg01Plant plant = payPolVers.getEese05PlantCodeC();

        exp = builder.get("eese05PlantCodeC").equal(plant);
        if (mode.equalsIgnoreCase("approvedOnly")) {
            exp = exp.and(builder.get("eese05PpyverApprovalUserC").notNull());
        }
        if (!supplierClass.equals("-1")) {
            exp = exp.and(builder.get("teese04Paypolicy")
                    .get("teess01Supplier").get("eess01SupplierClassCodeC")
                    .equal(supplierClass));
        }
        if (supplierCode != null && !StringUtils.isBlank(supplierCode)) {
            supplierCode += "%";
            exp = exp.and(builder.get("teese04Paypolicy")
                    .get("teess01Supplier").get("eess01SupplierCodeC").like(
                            supplierCode));
        }
        if (itemCat == null || itemCat != -1) {
            exp = exp.and(builder.get("teese04Paypolicy").get(
                    "teesc05Itemcatgry").get("eesc05ItemcatgryNumberK").equal(
                    itemCat));
        }
        if (item == null || item != -1) {
            exp = exp.and(builder.get("teese04Paypolicy").get("teesc06Item")
                    .get("eesc06ItemNumberK").equal(item));
        }
        return exp;
    }

}
